<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span id="flowsetname"></span><div class="move_title"></div><?php include "../public/subturn.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请人</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowuser" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">申请部门</label>
                        <div class="layui-input-block">
                            <input type="text" id="flowdept" readonly placeholder="请输入" autocomplete="off" class="layui-input readonly">
                        </div>
                    </div>
                </div>
            </div>


            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bu','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin.php?dom=enginid&page=contract_bu','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>主合同</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="shiid" class="layui-input shiid" onclick="choose_contract_shi()">
                            <div class="choose_div" id="shiid" key="1"></div>
                            <span class="choose_one" onclick="choose_contract_shi()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">甲方</label>
                        <div class="layui-input-block">
                            <input type="text" id="shi_faer" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">乙方</label>
                        <div class="layui-input-block">
                            <input type="text" id="shi_chenger" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>补充合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" id="name" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同编号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" readonly placeholder="请输入" clearable autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>合同金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">签订人</label>
                        <div class="layui-input-block">
                            <input type="text" name="qianer" id="qianer" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">签订日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="qiandt" id="qiandt" readonly placeholder="请选择" clearable  autocomplete="off" class="layui-input bdate_ymd">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">主要条款</label>
                        <div class="layui-input-block">
                            <textarea id="clause" name="clause" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">关联单据</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="flowrelas" class="layui-input flowrelas" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');">
                            <div class="choose_div" id="flowrelas" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_flowrelas.php?dom=flowrelas','80%','80%');"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="advanersDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label">预审人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"></div>
                                <div class="tanxiannei" id="advaners"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=advaners&type=user&single=2','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-hide" id="nexterDiv">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>指定下一步审核人</label>
                        <div class="layui-input-block relative">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="nexter"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=nexter&type=user&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>

        <div class="block-generic">
            <div class="layui-form-item height_auto">
                <div class="short_table_list" key="1">
                    <div class="opt-btn-group">
                        <div><span>清单明细</span></div>
                        <div>
                            <button type="button" onclick="ctrlv_data_open()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">粘贴新增</button>
                            <button type="button" onclick="import_list()" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn">Excel导入</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_copy_row">复制行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_add_row">插入行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_row">删除行</button>
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-primary mingxibtn short_table_del_all">删除全部</button>
                        </div>
                    </div>
                    <table class="layui-table" lay-size="sm">
                        <thead>
                        <tr>
                            <th width="45">序号</th>
                            <th>清单编号</th>
                            <th><text>*</text>项目名称</th>
                            <th>项目特征描述</th>
                            <th>单位</th>
                            <th>工程量</th>
                            <th>综合单价(元)</th>
                            <th>合价(元)</th>
                            <th>备注</th>
                        </tr>
                        </thead>
                        <tbody class="short_table_tbody" id="list_list"></tbody>
                    </table>
                    <div class="listerror">提示：<span></span></div>
                </div>
            </div>
        </div>
    </div>
</form>
<script>
    var parent_shiid = getQueryVariable('shiid') || 0;

    $("#commentForm").validate({
        rules: {
            name: {required: true},
            money: {required: true},
        },
        messages:{
            name: {required: "请输入"},
            money: {required: "请输入"},
        }
    });



    function choose_contract_shi(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_contract_shi.php?dom=shiid&page=contract_bu&enginid='+enginid,'80%','80%')
    }

    function import_list() {
        openPage('../public/import.php?reveal=contract_bu@importdata', '770px', '490px');
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_flowdata_add_contract_bu, data:{shiid: parent_shiid}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data;
                $("#flowsetname").text(data.flowsetname);
                $("#flowuser").val(data.flowuser);
                $("#flowdept").val(data.flowdept);
                $("#number").val(data.number);
                if(data.isadvan){
                    $("#advanersDiv").removeClass('layui-hide');
                }

                var shiinfo = res.data.shiinfo;
                if(shiinfo){
                    set_choose_btn('shiid',shiinfo.id,shiinfo.name);
                    $("#shi_name").val(shiinfo.name);
                    $("#shi_faer").val(shiinfo.faer);
                    $("#shi_chenger").val(shiinfo.chenger);
                    $("#name").val(shiinfo.name+'补充合同');
                }

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                }

                var listAddStr = '';
                listAddStr += '<tr>';
                listAddStr += '<td style="width: 45px">1</td>';
                listAddStr += '<td><input type="text" class="layui-input num"></td>';
                listAddStr += '<td><input type="text" class="layui-input name"></td>';
                listAddStr += '<td><input type="text" class="layui-input xiang"></td>';
                listAddStr += '<td><input type="text" class="layui-input unit"></td>';
                listAddStr += '<td><input type="text" class="layui-input bulk"></td>';
                listAddStr += '<td><input type="number" class="layui-input price"></td>';
                listAddStr += '<td><input type="number" class="layui-input heprice"></td>';
                listAddStr += '<td><input type="text" class="layui-input explain"></td>';
                listAddStr += '</tr>';
                short_table_opt_func(listAddStr,1);

                public_upload_file_load('upload_files');
                render_from();
                initParseMoney();
            }
        });


        form.on('submit(add)', function(data){
            data.field.isturn = $(this).attr('key') || 0;
            data.field.flowrelas = get_choose_val('flowrelas');
            data.field.advaners = tan_tags_getvalue("#advaners", 'user');
            data.field.nexter = tan_tags_getvalue("#nexter", 'user');
            data.field.enginid = get_choose_val('enginid');
            data.field.shiid = get_choose_val('shiid');

            if(data.field.isturn > 0){
                var validRes = $("#commentForm").valid();
                formValid('enginid');
                if(data.field.enginid == undefined || !data.field.enginid){
                    validRes = false;
                }

                formValid('shiid');
                if(data.field.shiid == undefined || !data.field.shiid){
                    validRes = false;
                }
                if(!validRes){return false;}
            }


            var list = [''];
            try {
                $('#list_list tr').each(function (i) {
                    var num = $(this).find('.num').val();
                    var name = $(this).find('.name').val();
                    var xiang = $(this).find('.xiang').val();
                    var unit = $(this).find('.unit').val();
                    var bulk = $(this).find('.bulk').val();
                    var price = $(this).find('.price').val();
                    var heprice = $(this).find('.heprice').val();
                    var explain = $(this).find('.explain').val();

                    var index = i + 1;
                    if(!num && !name && !xiang && !unit && !bulk && !price && !heprice && !explain){
                        return false;
                    }
                    if(!name){
                        if(data.field.isturn > 0){
                            throw new Error('第'+index+'行，请输入名称');
                        }
                    }

                    list.push({
                        num:num,
                        name:name,
                        xiang:xiang,
                        unit:unit,
                        bulk:bulk,
                        price:price,
                        heprice:heprice,
                        explain:explain,
                    });
                });
            } catch(e) {
                $(".listerror").find('span').text(e.message);
                $(".listerror").show();
                return false;
            }
            $(".listerror").hide();
            data.field.list = list;


            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);
            public_flow_submit(data.field, ask_flowdata_add_contract_bu, ask_flowdata_edit_contract_bu, $(this).attr('but'));
            return false;
        });

    });

    function update_refresh_row_func() {
        render_date();
        render_from();
        $.each($("#list_list").children('tr'),function (key,val) {
            $(this).children('td:first-child').html(key+1);
        });
        setTimeout(function () {
            $(".short_table_list table tbody tr").click(function () {
                $(".short_table_list table tbody tr").removeClass('line-row');
                $(".short_table_list table tbody tr").attr('sle','');
                $(this).addClass('line-row');
                $(this).attr('sle','sle');
            });
        },1000);
    }





    var ctrlv_field_name = [
        {field: 'num', name:'清单编号'},
        {field: 'name', name:'项目名称'},
        {field: 'xiang', name:'项目特征描述'},
        {field: 'unit', name:'单位'},
        {field: 'bulk', name:'工程量'},
        {field: 'price', name:'综合单价(元)',type:'number'},
        {field: 'heprice', name:'合价(元)',type:'number'},
        {field: 'explain', name:'备注'},
    ];
    function ctrlv_data_show(list) {
        if(list.length > 0){
            var nums = $("#list_list tr").length;
            for (var key in list){
                var str = '';
                nums++;
                str += '<tr>';
                str += '<td style="width: 45px">'+nums+'</td>';
                str += '<td><input type="text" class="layui-input num" value="'+list[key].num+'"></td>';
                str += '<td><input type="text" class="layui-input name" value="'+list[key].name+'"></td>';
                str += '<td><input type="text" class="layui-input xiang" value="'+list[key].xiang+'"></td>';
                str += '<td><input type="text" class="layui-input unit" value="'+list[key].unit+'"></td>';
                str += '<td><input type="text" class="layui-input bulk" value="'+list[key].bulk+'"></td>';
                str += '<td><input type="number" class="layui-input price" value="'+list[key].price+'"></td>';
                str += '<td><input type="number" class="layui-input heprice" value="'+list[key].heprice+'"></td>';
                str += '<td><input type="text" class="layui-input explain" value="'+list[key].explain+'"></td>';
                str += '</tr>';
                $("#list_list").append(str);
            }
        }
        render_date();
        render_from();
        update_refresh_row_func();
    }
</script>
</body>
</html>
